package com.z.system.dto;

import java.util.Set;
import java.util.UUID;

import io.swagger.v3.oas.annotations.media.Schema;

/**
 * 用户请求数据传输对象，用于接收前端发送的用户相关请求数据
 */
public class UserRequestDTO {
    @Schema(description = "用户ID", example = "123e4567-e89b-12d3-a456-426614174000", required = false)
    private String userId;

    @Schema(description = "用户名", example = "admin", required = true)
    private String username;

    @Schema(description = "密码", example = "password123", required = false)
    private String password;

    @Schema(description = "工号", example = "EMP001", required = false)
    private String userSn;

    @Schema(description = "邮箱", example = "admin@example.com", required = false)
    private String email;

    @Schema(description = "昵称", example = "管理员", required = false)
    private String nickname;

    @Schema(description = "手机号", example = "13800138000", required = false)
    private String phone;

    @Schema(description = "性别(0女，1男)", example = "1", required = false)
    private Integer sex;

    @Schema(description = "启用状态(true启用，false禁用)", example = "true", required = false)
    private Boolean enabled;

    @Schema(description = "备注", example = "系统管理员", required = false)
    private String remark;

    @Schema(description = "头像URL", example = "/avatars/default.png", required = false)
    private String headImgUrl;

    @Schema(description = "部门ID", example = "123e4567-e89b-12d3-a456-426614174001", required = false)
    private String deptId;

    @Schema(description = "角色ID集合", required = false)
    private Set<String> roleIds;

    // 将DTO转换为User实体
    public com.z.system.model.User toUser() {
        com.z.system.model.User user = new com.z.system.model.User();
        
        if (userId != null && !userId.isEmpty()) {
            user.setId(UUID.fromString(userId));
        }
        
        user.setUsername(username);
        user.setPassword(password);
        user.setUserSn(userSn);
        user.setEmail(email);
        user.setNickname(nickname);
        user.setPhone(phone);
        user.setSex(sex);
        user.setEnable(enabled != null ? (enabled ? 1 : 0) : null);
        user.setRemark(remark);
        user.setHeadImgUrl(headImgUrl);
        user.setDeptId(deptId != null && !deptId.isEmpty() ? UUID.fromString(deptId) : null);
        
        return user;
    }

    // 获取角色ID的UUID集合
    public Set<UUID> getRoleIdsAsUUID() {
        if (roleIds == null || roleIds.isEmpty()) {
            return null;
        }
        return roleIds.stream()
                .map(UUID::fromString)
                .collect(java.util.stream.Collectors.toSet());
    }

    // Getters and Setters
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserSn() {
        return userSn;
    }

    public void setUserSn(String userSn) {
        this.userSn = userSn;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getHeadImgUrl() {
        return headImgUrl;
    }

    public void setHeadImgUrl(String headImgUrl) {
        this.headImgUrl = headImgUrl;
    }

    public String getDeptId() {
        return deptId;
    }

    public void setDeptId(String deptId) {
        this.deptId = deptId;
    }

    public Set<String> getRoleIds() {
        return roleIds;
    }

    public void setRoleIds(Set<String> roleIds) {
        this.roleIds = roleIds;
    }
}